/**
 * Copyright 2008-2009. Chongqing Communications Industry Services Co.,Ltd Information Technology Branch. All rights
 * reserved. <a>http://www.cqcis.com</a>
 */
package com.cqcis.uip.base.framework.common;

import com.cqcis.uip.base.framework.utils.ByteBuffer;
import com.cqcis.uip.base.framework.utils.StringUtil;

/**
 * telnet 命令解析码
 * 
 * @author huangmy
 * @date   2008-8-14
 */
public class TelnetCommand {
    public final static byte IAC = (byte)0xFF; // telnet命令起始
    /** 协商控制 */
    public final static byte DONT = (byte)0xFE; // 选项协商, 拒绝, DONT
    public final static byte DO = (byte)0xFD; // 选项协商, 请求, DO
    public final static byte WONT = (byte)0xFC; // 选项协商, 不希望, WONT
    public final static byte WILL = (byte)0xFB; // 选项协商, 希望, WILL
    /** 选项 */
    public final static byte SB = (byte)0xFA; // 选项协商, 子选项开始
    public final static byte SE = (byte)0xF0; // 选项协商, 子选项结束
    /** 选项值 */
    public final static byte TERMIAL_TYPE = (byte)0x18; // 终端类型
    public final static byte PARAM_WINDOW = (byte)0x1F; // 窗口大小
    public final static byte PARAM_TERMINAL_SPEED = (byte)0x20; // 终端速率
    public final static byte PARAM_REMOTE_STREAM_CONTRL = (byte)0x21; // 远程流量控制
    public final static byte PARAM_LINE_MODE = (byte)0x22; // 行方式
    public final static byte PARAM_ENV = (byte)0x24; // 环境变量
    
    public final static byte ASK = (byte)0x01; // 询问
    public final static byte RSP = (byte)0x00; // 回答,是
    
    public final static String TELNET_CMD_RSP = "TELNET_CMD_RSP" + TelnetCommand.class.getName();
    
    private byte cmd; // 命令
    private byte param;// 参数, 即选项    
    private byte[] subCmdContext;// 子命令内容
    
    public TelnetCommand() {    	
    }
    
    public TelnetCommand(byte cmd, byte param) {
    	this.cmd = cmd;
    	this.param = param;
    }
    
    public TelnetCommand(byte cmd, byte param, byte[] subCmdContext) {
    	this.cmd = cmd;
    	this.param = param;
    	this.subCmdContext = subCmdContext;
    }
        
    @Override
    public String toString() {
    	byte[] bs = toArray();
    	StringBuffer buf = new StringBuffer();
    	for (byte b : bs) {
    		buf.append(StringUtil.toHexString(b, false)).append(" ");
    	}
    	
    	return buf.toString();
    }
    
    /**
     * 字节缓冲
     * 
     * @return
     */
    public ByteBuffer toByteBuffer() {
    	ByteBuffer buf = ByteBuffer.allocate(32).setAutoExpand(true);
    	if (this.cmd != SB) {
    		buf.put(IAC);
        	buf.put(this.cmd);
        	buf.put(this.param);
    	}
    	    	
    	if (this.subCmdContext != null) {
    		buf.put(IAC);
    		buf.put(SB);
    		buf.put(this.subCmdContext);
    		buf.put(IAC);
    		buf.put(SE);
    	}
    	
    	buf.flip();
    	
    	return buf;
    }
    
    /**
     * 数组
     * 
     * @return
     */
    public byte[] toArray() {
    	ByteBuffer buf = toByteBuffer();
    	if (buf != null) {
    		return buf.array();
    	}
    	return null;
    }

	public byte getCmd() {
		return cmd;
	}

	public void setCmd(byte cmd) {
		this.cmd = cmd;
	}

	public byte getParam() {
		return param;
	}

	public void setParam(byte param) {
		this.param = param;
	}

	public byte[] getSubCmdContext() {
		return subCmdContext;
	}

	public void setSubCmdContext(byte[] subCmdContext) {
		this.subCmdContext = subCmdContext;
	}
}
